package peer.p2pnet.message;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import peer.Message;
import peer.tools.ByteIntSwitch;
import peer.tools.ByteShortSwitch;

public class GetSNodeMessage2 extends Message{
	public short sType=peer.tools.MessageType.sType_GetSNodeMessage2;
	InetAddress sIp;
	int sPort;
	byte[] dpData=new byte[12];
	public GetSNodeMessage2(InetAddress sIp,int sPort){
		this.sIp=sIp;
		ByteShortSwitch.toByteArray(ver, dpData, 0);  //add: ver
		ByteShortSwitch.toByteArray(sType, dpData, 2);  //add: service type
		
		System.arraycopy(sIp.getAddress(), 0, dpData, 4, 4);  //add: sIp
		ByteIntSwitch.toByteArray(sPort, dpData, 8);  //add: sPort
		dp=new DatagramPacket(dpData,dpData.length);
	}
	public InetAddress getSIp(){
		return sIp;
	}
	
	public int getSPort (){
		return sPort ;
	}
	
	public GetSNodeMessage2(DatagramPacket dp){
		this.dp=dp;
		dpData=dp.getData();
		ver=ByteShortSwitch.toShort(dpData, 0);
		sType=ByteShortSwitch.toShort(dpData, 2);
		byte [] b=new byte[4];
		try {
			sIp=InetAddress.getByAddress(b);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		System.arraycopy(dpData, 4, b, 0, 4);
		sPort=ByteIntSwitch.toInt(dpData, 8);
	}

}
